import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../bean/BlockBean.dart';
import '../config/AppConst.dart';
import '../pages/GamePage.dart';
import '../wight/My.dart';
import 'SpUtils.dart';

/**
 * 获取每个小格子的大小，根据背景的宽度获取
 */
class CommUtils {
  static Size getBrikSizeForScreenWidth(double width) {
    return Size.square((width) / GAME_PAD_MATRIX_W - width * 0.01);
  }

  static double getWidth(BuildContext context) {
    return MediaQuery.of(context).size.width; //宽度
  }

  static double getHeight(BuildContext context) {
    return MediaQuery.of(context).size.height; //高度
  }

/**
 * true==有值
 */
  static bool validateInput(String? input) {
    return input?.isNotEmpty ?? false;
  }

  // double? playerPanelWidth = 0; //棋盘的宽度

  static Size eachSquare = Size(0, 0); //每个方格宽度
/**
 * 每个格子的宽度
 */
  static Size getSize(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    var playerPanelWidth;
    //size.width*0.02 * 2  棋盘左右边距，size.width*0.057 棋盘左右边距
    if (getISWidth(size.width, size.height)) {
      playerPanelWidth = size.height * 0.5;
    } else {
      playerPanelWidth =
          size.width - (size.width * 0.02 * 2 + size.width * 0.057);
    }
    eachSquare = CommUtils.getBrikSizeForScreenWidth(playerPanelWidth);
    return eachSquare;
  }

  static getISWidth(double width, double height) {
    if (width >= height - 250) {
      return true;
    } else {
      return false;
    }
  }

  static Future<String> getVerison() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    // String appName = packageInfo.appName;
    //String packageName = packageInfo.packageName;
    return packageInfo.version; //1.0.0
    // return packageInfo.buildNumber; //android-code
  }

/**
 * 获取普遍文字
 */
  static Widget getTextString(BuildContext context, String text, double sp,
      bool isBold, int maxLines, Color color) {
    return AutoSizeText(
      text,
      style: TextStyle(
        fontSize: sp,
        fontWeight: isBold ? FontWeight.bold : FontWeight.normal,
        color: color,
 
      ),
      maxLines: maxLines,
      textAlign: TextAlign.center,
      overflow: TextOverflow.ellipsis,
    );
  }

  static Widget getText(BuildContext context, double sp, String titel,
      {double maxFontSize = 50}) {
    return ShaderMask(
      shaderCallback: (Rect bounds) {
        return LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [
            textColor,
            textbgColor,
          ],
        ).createShader(bounds);
      },
       
      child: AutoSizeText(
        titel,
        maxFontSize: maxFontSize,
        style: TextStyle(
          fontSize: sp,
          fontWeight: FontWeight.bold,
          color: textColor,
          shadows: <Shadow>[
            Shadow(
              offset: Offset(1, 3),
              blurRadius: 2.0,
              color: Colors.black,
            ),
          ],
         
        ),
        maxLines: 1,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  static Widget getTextGame(BuildContext context, double sp, String titel,
      Color textColor, Color textbgColor) {
    return Padding(
      padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(6)),
      child: ShaderMask(
        shaderCallback: (Rect bounds) {
          return LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              textColor,
              textbgColor,
            ],
          ).createShader(bounds);
        },
        child: AutoSizeText(
          titel,
          style: TextStyle(
            fontSize: sp,
            fontWeight: FontWeight.bold,
            color: textColor,
            shadows: <Shadow>[
              Shadow(
                //阴影
                offset: Offset(1, 3),
                blurRadius: 2.0,
                color: Colors.black,
              ),
            ],
          
          ),
          maxLines: 1,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
        ),
      ),
    );
  }

//描边
  static Widget getStroke(
      String text, double sp, Color textColor, Color strokeColor) {
    return Stack(
      children: <Widget>[
        AutoSizeText(
          text,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            //   color: textColor, 否要要报错
            fontSize: sp,
            foreground: Paint()
              ..style = PaintingStyle.stroke
              ..strokeWidth = 2
              ..color = strokeColor,
        
          ),
          maxLines: 1,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
        ),
        // Stroked text as border.
        AutoSizeText(
          text,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize:sp,
            color: textColor,
           
          ),
          maxLines: 1,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
        ),
        // Solid text as fill.
      ],
    );
  }

  //描边
  static Widget getStroke2(String text, double sp, Color textColor,
      Color strokeColor, double strokeWidth, int maxLines) {
    return Stack(
      children: <Widget>[
        AutoSizeText(
          text,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            //   color: textColor, 否要要报错
            fontSize: sp,
            foreground: Paint()
              ..style = PaintingStyle.stroke
              ..strokeWidth = strokeWidth
              ..color = strokeColor,
         
          ),
          maxLines: maxLines,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
        ),
        // Stroked text as border.
        AutoSizeText(
          text,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: sp,
            color: textColor,
          
          ),
          maxLines: maxLines,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
        ),
        // Solid text as fill.
      ],
    );
  }

/**
 * 获取旅行当前等级
 */
  static int getGrade() {
    int? grade = SpUtils.getInstance()?.getInt2(AppConst.grade);
    if (grade != null) {
      return grade;
    } else {
      return 1;
    }
  }

  // 数组拷贝  对象深拷贝，以免修改一个list值影响到另外一个
  static List<List<BlockBean>> copyWithList(List<List<BlockBean>> list) {
    List<List<BlockBean>> copyList = [];

    for (var item in list) {
      List<BlockBean> _newdata = [];
      for (var item2 in item) {
        BlockBean bean = BlockBean();
        bean.defaultImg = item2.defaultImg;
        bean.eliminationEffect = item2.eliminationEffect;
        bean.img = item2.img;
        bean.normal = item2.normal;
        _newdata.add(bean);
      }

      copyList.add(_newdata);
    }
    return copyList;
  }
}
